package com.zxy.ziems.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.comparator.CompareUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.btp.common.exception.ConditionException;
import com.zxy.btp.common.util.IdWorker;
import com.zxy.ziems.server.domain.UserRoleRel;
import com.zxy.ziems.server.mapper.UserRoleRelMapper;
import com.zxy.ziems.server.service.UserRoleRelService;
import com.zxy.ziems.server.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName UserRoleRelServiceImpl
 * @Description
 * @Author 邓伟
 * @Date 2023/11/17 15:07
 * @Version 1.0
 */
@Service
public class UserRoleRelServiceImpl extends ServiceImpl<UserRoleRelMapper, UserRoleRel> implements UserRoleRelService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private UserRoleRelMapper userRoleRelMapper;
    @Override
    public void batchInsert(List<String> roleIdList, String userId) {
        QueryWrapper<UserRoleRel> query = new QueryWrapper<>();
        query.lambda().eq(UserRoleRel::getUserId,userId);
        userRoleRelMapper.delete(query);
        List<UserRoleRel> list = new ArrayList<>();
        for (String roleId : roleIdList) {
            UserRoleRel userRoleRel = new UserRoleRel();
            userRoleRel.setRoleId(roleId);
            userRoleRel.setUserRoleId(idWorker.nextId() +"");
            userRoleRel.setUserId(userId);
            list.add(userRoleRel);
        }
        saveBatch(list);
    }

    @Override
    public List<RoleVo> selectListByUserId(List<String> userIdList) {
        return userRoleRelMapper.selectListByUserId(userIdList);
    }

    @Override
    public void checkIsRel(String roleId) {
        Long count = userRoleRelMapper.selectCount(new QueryWrapper<UserRoleRel>()
                .lambda().eq(UserRoleRel::getRoleId, roleId));
        if (CompareUtil.compare(0, count.intValue()) != 0) {
            throw new ConditionException("角色已绑定用户，无法删除，请解除绑定后再试");
        }
    }

    @Override
    public List<String> selectRoleIdList(String userId) {
        QueryWrapper<UserRoleRel> query = new QueryWrapper<>();
        query.lambda().eq(UserRoleRel::getUserId,userId);
        List<UserRoleRel> relList = userRoleRelMapper.selectList(query);
        if (CollectionUtil.isNotEmpty(relList)) {
            return relList.stream().map(UserRoleRel::getRoleId).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<UserRoleRel> listByRoleIds(Set<String> roleIdSet) {
        if (CollectionUtil.isEmpty(roleIdSet)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<UserRoleRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserRoleRel::getRoleId,roleIdSet);
        return baseMapper.selectList(wrapper);
    }
}
